package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.QueryKeyFocusListReq;
import com.sg.entity.OmsKeyFocus;
import com.sg.service.base.app.MOmsKeyFocusService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsKeyFocusMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsKeyFocusServiceImpl
        extends ServiceImpl<OmsKeyFocusMapper, OmsKeyFocus>
        implements MOmsKeyFocusService {

    @Autowired
    OmsKeyFocusMapper omsKeyFocusMapper;

    /**
     * 3-16-01查询重点关注列表[8877]
     * gen by moon at 4/1/2024, 1:47:46 AM
     */
    @Trace(operationName = "3-16-01查询重点关注列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsKeyFocus> queryKeyFocusList(QueryKeyFocusListReq reqDto) {

        try {
            QueryWrapper<OmsKeyFocus> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsKeyFocus.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsKeyFocus.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsKeyFocus.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsKeyFocus>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-16-01新增重点关注任务[8878]
     * gen by moon at 7/26/2024, 1:59:30 PM
     */
    @Trace(operationName = "3-16-01新增重点关注任务")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addKeyFocusTask(OmsKeyFocus omsKeyFocus) {
        omsKeyFocus.setKeyFocusId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsKeyFocus.setSubjectLifeCycle("CURRENTLY_VALID");
        omsKeyFocus.setIsArchive("FALSE");
        omsKeyFocus.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsKeyFocus.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsKeyFocus.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsKeyFocus.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsKeyFocus.setIsValid("FALSE");
        omsKeyFocus.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsKeyFocus.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsKeyFocus)) {
            return omsKeyFocus.getKeyFocusId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-16-01删除重点关注任务[8879]
     * gen by moon at 7/26/2024, 1:59:31 PM
     */
    @Trace(operationName = "3-16-01删除重点关注任务")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteKeyFocusTask(String keyFocusId) {

        return super.removeById(keyFocusId);
    }

    /**
     * 3-16-01批量改重点关注[10062]
     * gen by moon at 8/6/2024, 7:26:14 PM
     */
    @Trace(operationName = "3-16-01批量改重点关注")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateKeyFocus(List<OmsKeyFocus> omsKeyFocus) {

        if (omsKeyFocus == null || omsKeyFocus.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsKeyFocus);

    }
}
